const DEVELOPMENT=1;
var controllList;
const JSON_TIMEOUT = 24000;
var JSON_URL = 'http://192.168.1.100:8080';
var JSON_SCRIPT = '/frinton/interface/frinton.php';

// DEVELOPMENT
if (DEVELOPMENT == 1) {
	JSON_URL = 'http://localhost';
//JSON_URL = 'http://localhost:8090';
	JSON_SCRIPT = '/frinton/web/interface/frinton.php';
}

// LIVE

/**
 * ********************************************************
 * 							OBJECTS
 * ********************************************************
 */


function Controll(aCategory, aName, aType, aStatus, anId, someInfectedControlls) {
	this.category = aCategory;
	this.name = aName;
	this.type = aType;
	this.status = aStatus;
	this.id = anId;
	this.infectedControlls = someInfectedControlls;
}

/**
 * ********************************************************
 * 							FUNCTIONS
 * ********************************************************
 */

function init() {
	receiveControlls();
}

function processEvent(aName) {
	var tmpControll = getControllByName(aName);
	tmpControll.status = switchOnOff(tmpControll.status);
	try {
		$.ajax({type: "POST",
			dataType: "json",
			url:    JSON_URL + JSON_SCRIPT,
			jsonpCallback: 'jsonCallback',
			data: {action: 'cmd',entries: JSON.stringify(tmpControll)},
			timeout: JSON_TIMEOUT,
			success: function(data) {
				updateInfectedControllsInGUI(tmpControll);
			},
			error: function(x, t, aMessage) {
				alert(aMessage);
			}
		});
	} catch(anError) {
		alert(anError);
	}
}

function switchOnOff(aState) {
	return aState=='on'?'off':'on';
}
function updateInfectedControllsInGUI(aControll) {
	$('#img' + aControll.name).attr("src","images/lampe" + aControll.status + ".png");
	$.each(aControll.infectedControlls, function(anIndex, anItem) {
		tempInnerControll = getControllByName(anItem.name);
		tempInnerControll.status = anItem.status;
//		$('#' + anItem.name).val(tempInnerControll.status).slider("refresh");
		$('#img' + anItem.name).attr("src","images/lampe" + anItem.status + ".png");
	});
}

function getControllByName(aName) {
	var tmpReturnItem;
	$.each(controllList, function(anIndex, anItem) {
		if (anItem.name == aName) {
			tmpReturnItem = anItem;
			return;
		}
	});
	return tmpReturnItem;
}

function createCategory(aCategoryName) {
	var tmpCategoryName = aCategoryName.replace(' ','_');

	var tmpHTML = '';
	tmpHTML += '<div data-role="collapsible" data-collapsed="true" id="' + tmpCategoryName + '">';
	tmpHTML += '<h2>' + aCategoryName + '</h2>';
	tmpHTML += '<ul data-role="listview" data-inset="false" id="list' + tmpCategoryName + '">';
	tmpHTML += '</ul></div>';
	$("#listSet").append(tmpHTML);
}

function createElementList() {

	$.each(controllList, function(anIndex, anItem) {
		var tmpControllName = anItem.name.replace(' ','_');
		var tmpCategoryName = anItem.category.replace(' ','_');

		var tmpCategory = document.getElementById('list' + tmpCategoryName);
		if (tmpCategory == null) {
			createCategory(anItem.category);
		}
		var tmpHTML = '';
		if (tmpCategoryName == 'Makros') {
			createButton( tmpControllName, tmpCategoryName)
		}
		else {
			createSliderElement( anItem, tmpControllName, tmpCategoryName)
		}
	});
	renderControlls();
}

function createButton( aControllName, aCategoryName) {
	tmpHTML = '<input id="' + aControllName + '" type="submit" value="' + aControllName + '" />';
	$("#list" + aCategoryName).append(tmpHTML);
	$('#' + aControllName).bind("click", function(event) {
		processEvent(event.target.id);
	});
}

function createSliderElement2( anItem, aControllName, aCategoryName) {
	tmpHTML = '<li class="ui-field-contain" data-inset="false">';
	tmpHTML += '<label for"' + aControllName + '">' + anItem.name + '</label>';
	tmpHTML += '<select data-role="slider" id="' + aControllName + '" name="' + aControllName + '">';
	tmpHTML += '<option value="on">on</option>';
	tmpHTML += '<option value="off">off</option>';
	tmpHTML += '</select>';
	tmpHTML += '</li>';
	$("#list" + aCategoryName).append(tmpHTML);
	$('#' + aControllName).val(anItem.status);
	$('#' + aControllName).slider().on("slidestop", function(event) {
		processEvent(event.target.id);
	});
}

function createSliderElement( anItem, aControllName, aCategoryName) {
	tmpHTML = '<li id="' + aControllName + '" class="ui-field-contain" data-inset="false">';
	tmpHTML += '<img style="float: left; margin-right: 20px;" id="img' + aControllName + '" src="images/lampe' + anItem.status + '.png" />';
	tmpHTML += '<h3>' + anItem.name + '</h3>';
	tmpHTML += '</li>';
	$("#list" + aCategoryName).append(tmpHTML);
	$("#listElement" + aControllName).css("min-height", "40px");
	$("#listElement" + aControllName).css("padding", "");
	$('#' + aControllName).bind("click", function(event) {
		processEvent(event.currentTarget.id);
	});
}

function renderControlls() {
	// rendern
	var tempCategories = new Array();
	$.each(controllList, function(anIndex, anItem) {
		var tmpCategoryName = anItem.category.replace(' ','_');
		if (tmpCategoryName == 'Makros') {
			var tmpName = anItem.name.replace(' ','_');
			$('#' + tmpName).button();
		}
		if (tempCategories[tmpCategoryName] != 1) {
			$('#' + tmpCategoryName).collapsible();
			$("#list" + tmpCategoryName).listview();
			tempCategories[tmpCategoryName] = 1;
		}
	});
}

function receiveControlls() {
	try {
		$.ajax({type: "POST",
			dataType: "json",
			url:    JSON_URL + JSON_SCRIPT,
			jsonpCallback: 'jsonCallback',
			data: {action: 'list'},
			timeout: JSON_TIMEOUT,
			success: function(data) {
				controllList = data;
				createElementList();
			},
			error: function(x, t, aMessage) {
			}
		});
	} catch(anError) {
		alert(anError);
	}
}
